<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
    <script th:src="@{/static/jquery-2.1.4.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/mylayer.js}" type="text/javascript" charset="utf-8"></script>
    <style>
        body, html {
            height: 100%;
            margin: 0;
            padding: 0;
        }

        .bottom-buttons {
            position: absolute;
            bottom: 100px;
            text-align: left;
            margin-left: 50px;
        }
    </style>
</head>
<body>
    <div style="text-align: left; margin-left: 50px">
        <div style="height: 20px;">

        </div>
        <div style="height: 50px; margin-left: 50px">
            <button type="button" class="layui-btn layui-bg-blue layui-btn-sm" id="addShipping">新建收货地址</button>
        </div>
        <div class="layui-input-block" id="shippingList" style="text-align: left; margin-left: 50px">

        </div>
    </div>
    <div class="bottom-buttons">
        <div style="height: 20px; margin-left: 50px">
            <button type="button" class="layui-btn layui-btn-sm" id="modifyAddress">修改地址</button>
            <button class="layui-btn layui-btn-primary layui-border layui-btn-sm" id="cancel">取消</button>
        </div>
    </div>
    <script>
        var queryString = window.location.search;
        var urlParams = new URLSearchParams(queryString);
        var orderNo = urlParams.get("orderNo");
        $.post(
            '/order/orderByOrderNo',
            {'orderNo': orderNo},
            function (result) {
                if (result.code == 0) {
                    $(result.data).each(function () {
                        var shippingId = this.shippingId;
                        $.post(
                            '/shipping/ListByUerId',
                            function (result) {
                                if (result.code == 0) {
                                    var html = '';
                                    $('#shippingList').empty();
                                    $(result.data).each(function () {
                                        if (this.id == shippingId) {
                                            html += '<p><input type="radio" name="shiping" value="'+this.id+'" checked="">'
                                        } else {
                                            html += '<p><input type="radio" name="shiping" value="'+this.id+'">'
                                        }

                                        html += ''+this.receiverProvince+''
                                        if (this.receiverCity === '市辖区'|| this.receiverCity === '县') {
                                            html += ' '
                                        } else {
                                            html += ''+this.receiverCity+''
                                        }
                                        html += ''+this.receiverArea+' '+this.receiverAddress+'（'+this.receiverZip+'）, （'+this.receiverName+' 收） '+this.receiverMobile+'</p>'
                                    });
                                    $('#shippingList').append(html);
                                }
                            },
                            'json'
                        );
                    });
                }
            },
            'json'
        );

        $('#modifyAddress').click(function () {
            $.post(
                '/order/update',
                {'orderNo': orderNo,'shippingId': $("input[name='shiping']:checked").val()},
                function (result) {
                    console.log(result);
                    if (result.code == 0) {
                        mylayer.okMsg(result.msg);
                        setInterval(function() {
                            //关闭弹出框
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            //刷新父页面
                            window.parent.location.reload();
                        }, 2000)
                    }
                },
                'json'
            );
        });
        $('#cancel').click(function () {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        });
        $('#addShipping').click(function () {
            layer.open({
                type: 2,
                title: '新建收货地址',
                area: ['100%', '100%'],// 宽高
                content: '/page/shipping/add',
            });
        });
    </script>
</body>
</html>